<template>
  <div class="login-page">
    <div class="login-container">
      <div class="logo-section">
        <h1>微信服务号</h1>
        <p>请登录后继续使用</p>
      </div>
      
      <t-form ref="formRef" :data="formData" @submit="handleLogin">
        <t-form-item label="用户名" name="username">
          <t-input
            v-model="formData.username"
            placeholder="请输入用户名"
            clearable
          />
        </t-form-item>
        
        <t-form-item label="密码" name="password">
          <t-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            clearable
          />
        </t-form-item>
        
        <t-form-item>
          <t-button
            theme="primary"
            type="submit"
            :loading="loading"
            block
          >
            登录
          </t-button>
        </t-form-item>
      </t-form>
      
      <div class="tips">
        <p>提示：表单页面可以直接访问，无需登录</p>
        <t-button variant="text" @click="goToForm">
          直接填写表单
        </t-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { MessagePlugin } from 'tdesign-mobile-vue'

const router = useRouter()
const authStore = useAuthStore()

const loading = ref(false)
const formData = reactive({
  username: '',
  password: ''
})

const handleLogin = async () => {
  if (!formData.username || !formData.password) {
    MessagePlugin.error('请输入用户名和密码')
    return
  }
  
  loading.value = true
  
  try {
    // 模拟登录请求
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 简单的登录验证（实际项目中应该调用后端接口）
    if (formData.username === 'admin' && formData.password === '123456') {
      authStore.login({
        id: '1',
        username: formData.username,
        name: '管理员'
      })
      
      MessagePlugin.success('登录成功')
      
      // 跳转到目标页面或首页
      const redirect = router.currentRoute.value.query.redirect as string
      router.push(redirect || '/')
    } else {
      MessagePlugin.error('用户名或密码错误')
    }
  } catch (error) {
    MessagePlugin.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const goToForm = () => {
  router.push('/form')
}
</script>

<style lang="less" scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  background: white;
  border-radius: 12px;
  padding: 40px 30px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
  
  h1 {
    font-size: 28px;
    color: #333;
    margin-bottom: 8px;
  }
  
  p {
    color: #666;
    font-size: 14px;
  }
}

.tips {
  margin-top: 30px;
  text-align: center;
  
  p {
    font-size: 12px;
    color: #999;
    margin-bottom: 10px;
  }
}
</style>